package com.qyp.content.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSON;
import com.qyp.content.domain.entity.messaging.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * @author admin
 */
@Slf4j
public class SentinelRestTemplateBlockHandler {

    /**不起作用*/
    public static SentinelClientHttpResponse block(HttpRequest httpRequest, byte[] body,
                                                   ClientHttpRequestExecution clientHttpRequestExecution,
                                                   BlockException e){
//        String errorMsg = "Unknown";
//        // 根据异常类型区分违反哪种规则
//        if(e instanceof FlowException){// 限流异常
//            errorMsg = "被限流规则阻挡";
//        }else if(e instanceof DegradeException){ // 降级异常
//            errorMsg = "被降级规则阻挡";
//        }else if(e instanceof ParamFlowException){ // 热点参数异常
//            errorMsg = "被热点参数规则阻挡";
//        }else if(e instanceof SystemBlockException){ // 系统规则异常
//            errorMsg = "被系统规则阻挡";
//        }else if(e instanceof AuthorityException){ // 认证异常
//            errorMsg = "被授权规则阻挡";
//        }
//        log.warn("block url:{},{}",httpRequest.getURI().toString(),errorMsg);
//        return new SentinelClientHttpResponse(errorMsg);

        ResponseVO sentinelErrorMsg = null;
        // 根据异常类型区分违反哪种规则
        if(e instanceof FlowException){// 限流异常
            sentinelErrorMsg = ResponseVO.builder()
                    .code(100)
                    .msg("被限流规则阻挡")
                    .build();
        }else if(e instanceof DegradeException){ // 降级异常
            sentinelErrorMsg = ResponseVO.builder()
                    .code(101)
                    .msg("被降级规则阻挡")
                    .build();
        }else if(e instanceof ParamFlowException){ // 热点参数异常
            sentinelErrorMsg = ResponseVO.builder()
                    .code(102)
                    .msg("被热点参数规则阻挡")
                    .build();
        }else if(e instanceof SystemBlockException){ // 系统规则异常
            sentinelErrorMsg = ResponseVO.builder()
                    .code(103)
                    .msg("被系统规则阻挡")
                    .build();
        }else if(e instanceof AuthorityException){ // 认证异常
            sentinelErrorMsg = ResponseVO.builder()
                    .code(104)
                    .msg("被授权规则阻挡")
                    .build();
        }else{
            sentinelErrorMsg = ResponseVO.builder()
                    .code(999)
                    .msg("Unknown")
                    .build();
        }
        log.warn("block url:{},{}",httpRequest.getURI().toString(),sentinelErrorMsg.toString());
        return new SentinelClientHttpResponse(JSON.toJSONString(sentinelErrorMsg));
    }

    /**
     * 熔断后处理的方法
     * @param request
     * @param body
     * @param execution
     * @param ex
     * @return
     */
    public static SentinelClientHttpResponse fallback(HttpRequest request, byte[] body,
                                                      ClientHttpRequestExecution execution,
                                                      BlockException ex) {
        System.err.println("fallback: " + ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("custom fallback info");
    }

}

